package com.tbdgroup.first.babytools;

import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;


@SuppressLint("SimpleDateFormat")
public class MainActivity extends Activity{
    TSEntryDbHelper dbhelper;
    public static long currentSleepEntryRow = -1;
    public static long currentEatEntryRow = -1;
    
	public final static String EXTRA_MESSAGE_TS = "com.tbdgroup.first.babytools.TSENTRYID";
	public final static String EXTRA_MESSAGE_TYPE = "com.tbdgroup.first.babytools.BABYID";
	public final static String TYPE_PEE = "Pee";
	public final static String TYPE_POO = "Poo";
	public final static String TYPE_SLEEP = "Sleep";
//	public final static String TYPE_SLEEP_END = "Sleep Ends";
	public final static String TYPE_EAT = "Eat";
//	public final static String TYPE_EAT_END = "Eat Ends";
	public final static String TEST_BABY_ID = "0000";

	/** Called when the user clicks the pee button */
	public void recordPeeTS(View view) {
	//	Intent intent = new Intent(this, DisplayTSActivity.class);
	    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");       
	    String date = sDateFormat.format(new java.util.Date());    
	//	intent.putExtra(EXTRA_MESSAGE_TS, date);

	//	Button bt =(Button)findViewById(R.id.pee_button);
		/*intent.putExtra(EXTRA_MESSAGE_TYPE, bt.getText());
		startActivity(intent);*/
	    dbhelper.putStartTS(TYPE_PEE, date, TEST_BABY_ID);
	    
		TextView textView1 = (TextView) findViewById(R.id.peeClock);
	//	TextView textView2 = (TextView) findViewById(R.id.pooClock);
	    textView1.setText(date);
	//    textView2.setText("");
	}
	
	/** Called when the user clicks the poo button */
	public void recordPooTS(View view) {
		    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");       
		    String date = sDateFormat.format(new java.util.Date());    		    

		    dbhelper.putStartTS(TYPE_POO, date, TEST_BABY_ID);
		    
			TextView textView1 = (TextView) findViewById(R.id.pooClock);
	//		TextView textView2 = (TextView) findViewById(R.id.peeClock);
		    textView1.setText(date);
	//	    textView2.setText("");
		}
	
	/** Called when the user clicks display report button */
	public void displayTSReport(View view) {
		Intent intent = new Intent(this, DisplayTSActivity.class);
		startActivity(intent);		
	}
	
	public void cleanDB(View view) {
		dbhelper.cleanDB();
	}
	
	/** Called when the user clicks sleep start/sleep end button button */
	public void switchSleep(View view){
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");       
	//	String currentDate = sDateFormat.format(new java.util.Date()); 

		//get current status for sleep
		Button bt = (Button) findViewById(R.id.sleep_button);
		String curSleepState = bt.getText().toString();
		if(curSleepState == null){
			return;
		}
		if(curSleepState.equals("Sleep Ends")){
			//sleep already starts, this click will end it
			bt.setText(R.string.sleep_start_button);
			Chronometer mChrn = (Chronometer) findViewById(R.id.sleepChronometer);
			mChrn.stop();
			if(currentSleepEntryRow > -1){
				String currentDate = sDateFormat.format(new java.util.Date()); 
				dbhelper.upateEndTS(currentDate,currentSleepEntryRow);
			}
			currentSleepEntryRow = -1;
		}else{
			//last sleep already ends, this click will start a new one
			bt.setText(R.string.sleep_end_button);
			Chronometer mChrn = (Chronometer) findViewById(R.id.sleepChronometer);
			mChrn.setBase(SystemClock.elapsedRealtime());
			mChrn.start();
			String currentDate = sDateFormat.format(new java.util.Date()); 
			currentSleepEntryRow = dbhelper.putStartTS(TYPE_SLEEP, currentDate, TEST_BABY_ID);
		}

	}
	
	/** Called when the user clicks eat start/eat end button button */
	public void switchEat(View view){
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");       

		//get current status for eat
		Button bt = (Button) findViewById(R.id.eat_button);
		String curEatState = bt.getText().toString();
		if(curEatState == null){
			return;
		}
		if(curEatState.equals("Eat Ends")){
			//eat already starts, this click will end it
			bt.setText(R.string.eat_start_button);
			Chronometer mChrn = (Chronometer) findViewById(R.id.eatChronometer);
			mChrn.stop();
			if(currentEatEntryRow > -1){
				String currentDate = sDateFormat.format(new java.util.Date()); 
				dbhelper.upateEndTS(currentDate,currentEatEntryRow);
			}

			currentEatEntryRow = -1;
		}else{
			//last eat already ends, this click will start a new one
			bt.setText(R.string.eat_end_button);
			Chronometer mChrn = (Chronometer) findViewById(R.id.eatChronometer);
			mChrn.setBase(SystemClock.elapsedRealtime());
			mChrn.start();
			String currentDate = sDateFormat.format(new java.util.Date()); 
			currentEatEntryRow = dbhelper.putStartTS(TYPE_EAT, currentDate, TEST_BABY_ID);
		}

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbhelper = new TSEntryDbHelper(getBaseContext());
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
	//	getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}


	
}
